package net.child.chess.pieces;

import java.util.List;
import net.child.chess.R;
import net.child.chess.utils.ChessDef;
import net.child.chess.utils.Move;
import net.child.chess.utils.Position;

public class BlackPown extends Pown
{
	public BlackPown(int position)
	{
		super(ChessDef.Color.BLACK, initialState(position), R.drawable.ic_black_pown);		
	}
	
	@Override 
	public void attacks(List<Integer> squares, IMoves iMoves)
	{
		//	Legal right diagonal attack. 
		Position toPosition = new Position(position());
		if(toPosition.incX() && toPosition.incY())
		{
			addAttackIfValid(squares, toPosition.position(), iMoves);
		}
		
		//	Legal left diagonal attack.
		toPosition.position(position());
		if(toPosition.decX() && toPosition.incY())
		{
			addAttackIfValid(squares, toPosition.position(), iMoves);
		}
		
		addCaptureInPassing(squares, iMoves);
	}
	
	@Override
	public void moves(List<Integer> squares, IMoves iMoves)
	{	
		//	Legal right diagonal movement. 
		Position toPosition = new Position(position());
		if(toPosition.incX() && toPosition.incY())
		{
			if(iMoves.isOpponentSquare(color(), toPosition.position()))
			{
				addMoveIfValid(squares, toPosition.position(), iMoves);
			}
		}
		
		//	Legal left diagonal movement.
		toPosition.position(position());
		if(toPosition.decX() && toPosition.incY())
		{
			if(iMoves.isOpponentSquare(color(), toPosition.position()))
			{
				addMoveIfValid(squares, toPosition.position(), iMoves);
			}
		}
		
		addCaptureInPassing(squares, iMoves);
		
		//	Legal forward movements.
		toPosition.position(position());
		if(toPosition.incY())
		{
			if(iMoves.isEmptySquare(toPosition.position()))
			{
				addMoveIfValid(squares, toPosition.position(), iMoves);
				//	check if this is the first move, we are checking 2 because
				//	we already did an increment.
				if(toPosition.y() == 2)
				{
					toPosition.incY();
					if(iMoves.isEmptySquare(toPosition.position()))
					{
						addMoveIfValid(squares, toPosition.position(), iMoves);
					}
				}
			}
		}	
	}
	
	@Override
	public boolean shouldBePromoted() 
	{
		return (new Position(position()).y() == 7);
	}
	
	static private PieceState initialState(int position)
	{
		Position pos = new Position(position); 
		return new PieceState(pos, pos.y() != 1);
	}
	
	//	Check if it is "capture in passing"
	//	TODO - this is a duplicate code.
	@Override
	public Move getExtraMove(IMoves iMoves)	
	{
		//	get info on the last pown movement
		List<MoveInfo> moves = iMoves.currentMove();
		if(moves.size() != 1)
		{	//	 only one movement expected in "capture in passing"
			return null;
		}
		MoveInfo move = moves.get(0);
		Position toPosition = new Position(move.to());
		Position fromPosition = new Position(move.from());
		if(toPosition.x() == fromPosition.x())
		{
			return null;
		}
		toPosition.decY();
		return new Move(toPosition.position(), ChessDef.KILL_MOVE);
	}
	
	
	private void addCaptureInPassing(List<Integer> squares, IMoves iMoves)
	{
		if(position().y() != 4)
		{
			return;
		}
			
		List<MoveInfo> prevMoves = iMoves.previousMove();
		//	there is no history.
		if(prevMoves == null)
		{
			Position pos = new Position(position().x() + 1, 4);
			if(pos.isValid())
			{
				if(iMoves.isPieceExist(Piece.Type.POWN, ChessDef.Color.WHITE, pos.position()) &&
				   iMoves.isEmptySquare(pos.position(position().x() + 1, 5).position()) &&
				   iMoves.isEmptySquare(pos.position(position().x() + 1, 6).position()))
				{
					addMoveIfValid(squares, pos.position(position().x() + 1, 5).position(), iMoves);
				}
			}
			pos.position(position().x() - 1, 4);
			if(pos.isValid())
			{
				if(iMoves.isPieceExist(Piece.Type.POWN, ChessDef.Color.WHITE, pos.position()) &&
				   iMoves.isEmptySquare(pos.position(position().x() - 1, 5).position()) &&
				   iMoves.isEmptySquare(pos.position(position().x() - 1, 6).position()))
				{
					addMoveIfValid(squares, pos.position(position().x() - 1, 5).position(), iMoves);
				}
			}
		}
		else if(prevMoves.size() == 1)
		{
			MoveInfo move = prevMoves.get(0);
			Position pos = new Position(position().x() + 1, 4);
			if(pos.isValid())
			{
				int toPosition = pos.position();
				int fromPosition = pos.position(position().x() + 1, 6).position();
				if(move.isSameMove(Piece.Type.POWN, ChessDef.Color.WHITE, fromPosition, toPosition))
				{
					addMoveIfValid(squares, pos.position(position().x() + 1, 5).position(), iMoves);
				}
			}
			
			pos.position(position().x() - 1, 4);
			if(pos.isValid())
			{
				int toPosition = pos.position();
				int fromPosition = pos.position(position().x() - 1, 6).position();
				if(move.isSameMove(Piece.Type.POWN, ChessDef.Color.WHITE, fromPosition, toPosition))
				{
					addMoveIfValid(squares, pos.position(position().x() - 1, 5).position(), iMoves);
				}
			}
		}
	}
}

